package me.yingjie.common.components

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material.Icon
import androidx.compose.material.OutlinedButton
import androidx.compose.material.Text
import androidx.compose.material.TextField
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Done
import androidx.compose.material.icons.outlined.Edit
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

fun dateFormatter(): DateTimeFormatter {
    return DateTimeFormatter.ofPattern("HH:mm:ss")
}

@Composable
fun TrackingNote(
    index: Int, isFinished: Boolean,noteList: SnapshotStateList<String>, startTimeList: ArrayList<LocalDateTime>,
    finishTimeList: SnapshotStateList<LocalDateTime>
) {
    TextField(
        modifier = Modifier.fillMaxSize(),
        value = noteList[index],
        onValueChange = { newText -> noteList[index] = newText },
        enabled = !isFinished,
        label = {
            val startTime = startTimeList[index].format(dateFormatter())
            val finishTime =
                if (finishTimeList[index] == LocalDateTime.MAX) "" else finishTimeList[index].format(
                    dateFormatter()
                )
            Text("${startTime}-${finishTime}")
        }
    )
}

@Composable
fun AddTaskButton(addNote: () -> Unit, addStartTime: () -> Unit, addFinishTime: () -> Unit, addIsFinished: () -> Unit) {
    OutlinedButton(
        content = { Text("新增", textAlign = TextAlign.Center) },
        modifier = Modifier.fillMaxSize(),
        onClick = {
            addNote()
            addStartTime()
            addFinishTime()
            addIsFinished()
        }
    )
}

@Composable
fun StateIcon(isFinished:Boolean){
    if (isFinished){
        Icon(Icons.Outlined.Edit, "修改")
    }else{
        Icon(Icons.Outlined.Done, "完成")
    }
}

@Composable
fun TaskStateButton(finishTime: () -> Unit, markFinished: () -> Unit, currentState: @Composable () -> Unit) {
    OutlinedButton(
        modifier = Modifier.fillMaxSize(),
        onClick = { finishTime();markFinished() },
        content = {
            currentState()
        }
    )
}

@Composable
fun TrackingBoard(
    startTimeList: ArrayList<LocalDateTime>,
    finishTimeList: SnapshotStateList<LocalDateTime>,
    noteList: SnapshotStateList<String>,
    isFinishedList: SnapshotStateList<Boolean>
) {
    Row {
        LazyRow(modifier = Modifier.weight(5f)) {
            itemsIndexed(noteList) { index, _ ->
                Column(modifier = Modifier.width(300.dp)) {
                    Row(modifier = Modifier.fillMaxHeight().weight(15f)) {
                        TrackingNote(index, isFinishedList[index],noteList, startTimeList, finishTimeList)
                    }
                    Row(modifier = Modifier.fillMaxHeight().weight(1f)) {
                        TaskStateButton(finishTime = { finishTimeList[index] = LocalDateTime.now() },
                            markFinished = { isFinishedList[index] = !isFinishedList[index] },
                            currentState = { StateIcon(isFinishedList[index]) })
                    }
                }
                Spacer(modifier = Modifier.width(20.dp))
            }
        }
        Column(modifier = Modifier.weight(0.4f)) {
            AddTaskButton(
                addNote = { noteList.add("") },
                addStartTime = { startTimeList.add(LocalDateTime.now()) },
                addFinishTime = { finishTimeList.add(LocalDateTime.MAX) },
                addIsFinished = { isFinishedList.add(false) }
            )
        }
    }
}
